package recanto.das.pedrinhas;

import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.util.ArrayList;
import java.util.List;
import javax.swing.*;
import recanto.das.pedrinhas.bean.Cliente;

public class SearchBoxModel extends AbstractListModel
                implements ComboBoxModel, KeyListener, ItemListener
{
    public int selectedIndex;
    public ArrayList<String> db = new ArrayList<String>();
    public ArrayList<String> data = new ArrayList<String>();
    public String selection;
    public JComboBox cb;
    public JComboBox cbAux = new JComboBox();
    public ComboBoxEditor cbe;
    public int currPos = 0;

    public SearchBoxModel(JComboBox jcb, List<Cliente> clientes)
    {

        cb = jcb;
        cbe = jcb.getEditor();
//here we add the key listener to the text field that the combobox is wrapped around
        cbe.getEditorComponent().addKeyListener(this);
        String [] nomes = new String[clientes.size()];
        for (int i = 0 ; i < clientes.size() ; ++i) {
            nomes[i] = clientes.get(i).getNome();
            db.add(clientes.get(i).getNome());
            data.add(clientes.get(i).getNome());
        }
        
        cbAux = new JComboBox(nomes);
      
        

//set up our "database" of items - in practice you will usually have a proper db.
       
    }
    public int getSelectedIndex() {
        return this.selectedIndex;
    }

    public void updateModel(String in)
    {
        data.clear();
//lets find any items which start with the string the user typed, and add it to the popup list
//here you would usually get your items from a database, or some other storage...
        for(String s:db)
            if(s.startsWith(in))
                data.add(s);

        super.fireContentsChanged(this, 0, data.size());

//this is a hack to get around redraw problems when changing the list length of the displayed popups
        cb.hidePopup();
        cb.showPopup();
        if(!data.isEmpty())
            cb.setSelectedIndex(0);
        

        System.out.println("updateModel");
    }

    @Override
    public int getSize(){return data.size();}
    @Override
    public Object getElementAt(int index){return data.get(index);}
    @Override
    public void setSelectedItem(Object anItem)
                                 {selection = (String) anItem;}
    @Override
    public Object getSelectedItem(){return selection;}
    @Override
    public void keyTyped(KeyEvent e){}
    @Override
    public void keyPressed(KeyEvent e){}

    @Override
    public void keyReleased(KeyEvent e)
    {
        String str = cbe.getItem().toString();
        JTextField jtf = (JTextField)cbe.getEditorComponent();
        currPos = jtf.getCaretPosition();

        if(e.getKeyChar() == KeyEvent.CHAR_UNDEFINED)
        {
            if(e.getKeyCode() != KeyEvent.VK_ENTER )
            {
                cbe.setItem(str);
                jtf.setCaretPosition(currPos);  
            }
            
        }
        else if(e.getKeyCode() == KeyEvent.VK_ENTER) {
            cb.setSelectedIndex(cb.getSelectedIndex());
            System.out.println("else Enter do KeyEvent " + this.selectedIndex);
        }
        else
        {
            updateModel(cb.getEditor().getItem().toString());
            cbe.setItem(str);
            jtf.setCaretPosition(currPos);
            
            System.out.println("else final do KeyEvent " + this.selectedIndex);
        }
    }

    @Override
    public void itemStateChanged(ItemEvent e)
    {
        cbe.setItem(e.getItem().toString());
        cb.setSelectedItem(e.getItem());
        
        // cbAux é um ComboBox Auxiliar para marcar em qual posicao esta o cliente selecionado
        // Esta posicao é a mesma em que o Cliente esta localizado no ArrayList de Clientes
        cbAux.setSelectedItem(e.getItem());
        this.selectedIndex = cbAux.getSelectedIndex();
        System.out.println("Item State Changed " + this.selectedIndex);
    }

}